##
rm(list=ls())
gc()

library(ggplot2)
library(scales)
library(data.table)
library(stringr)
library(dplyr)
library(gt)
library(tidyr)
library(tibble)
library(readr)



files = c('panel-analysis-2008-2012.csv.gz',
          'panel-analysis-2012-2016.csv.gz',
          'panel-analysis-2016-2020.csv.gz')



data = rbindlist(lapply(files, FUN = function(x){
  d=fread(x, select = c(
                        'Party_year1','Party_year2','DemDiff','RepDiff', 
                        'DemSpExp_nohh_year1','RepSpExp_nohh_year1',
                        'DemSpExpDiff_nohh', 'RepSpExpDiff_nohh', 
                        'Race','Gender','Age_year1',
                        'HomeownerBlockGroup_year1',
                        'WhiteBlockGroup_year1',
                        'HHIncomeBlockGroup_year1'))[,Years:=str_replace(str_replace(x, '.csv.gz', ''),'panel-analysis-','')]
  
  return(d)
}),fill=T)



### make party switching categories
data[!Party_year1%in%c('Republican','Democrat'),Party_year1:='Non-Partisan']

data[DemDiff==1|(DemDiff==0&Party_year1=='Democrat'),Party_year2:='Democrat']
data[RepDiff==1|(RepDiff==0&Party_year1=='Republican'),Party_year2:='Republican']
data[is.na(Party_year2),Party_year2:='Non-Partisan']

data[Party_year1!='Democrat' & Party_year2=='Democrat',Status:='Switch Democrat']
data[Party_year1=='Democrat' & Party_year2=='Democrat',Status:='Stable Democrat']

data[Party_year1!='Republican' & Party_year2=='Republican',Status:='Switch Republican']
data[Party_year1=='Republican' & Party_year2=='Republican',Status:='Stable Republican']

data[Party_year1!='Non-Partisan' & Party_year2=='Non-Partisan',Status:='Switch Non-Partisan']
data[Party_year1=='Non-Partisan' & Party_year2=='Non-Partisan',Status:='Stable Non-Partisan']




## Make table 

data[,Democrat:=as.numeric(Party_year1=='Democrat',na.rm=T)]
data[,Republican:=as.numeric(Party_year1=='Republican',na.rm=T)]
data[,Party_year1:=NULL]
data[Gender=='',Gender:=NA]
data[,Female:=as.numeric(Gender=='FEMALE')]
data[,Gender:=NULL]
data[,White:=as.numeric(Race=='White')]
data[,Race:=NULL]
data[Age_year1>120|Age_year1<18,Age_year1:=NA]
gc()

p1=data[Years=='2008-2012']
p2=data[Years=='2012-2016']
p3=data[Years=='2016-2020']


d1=p1[!is.na(Status),list(Age=round(mean(Age_year1,na.rm=T)),
                            Female=mean(Female,na.rm=T),
                            White=mean(White,na.rm=T),
                          `2008 Dem. Exp.`=mean(DemSpExp_nohh_year1,na.rm=T),
                          `2008 Rep. Exp.`=mean(RepSpExp_nohh_year1,na.rm=T),
                          `\\Delta Dem. Exp.`=mean(DemSpExpDiff_nohh,na.rm=T),
                          `\\Delta Rep. Exp.`=mean(RepSpExpDiff_nohh,na.rm=T),
                            `Block Group White`=mean(WhiteBlockGroup_year1,na.rm=T),
                            `Block Group Med. HH Inc.`=round(mean(HHIncomeBlockGroup_year1,na.rm=T)),
                            `Block Group Homeowner`=mean(HomeownerBlockGroup_year1,na.rm=T)
                            
         
        ),by='Status']



## p2



d2=p2[!is.na(Status),list(Age=round(mean(Age_year1,na.rm=T)),
                            Female=mean(Female,na.rm=T),

                            White=mean(White,na.rm=T),
                          `2012 Dem. Exp.`=mean(DemSpExp_nohh_year1,na.rm=T),
                          `2012 Rep. Exp.`=mean(RepSpExp_nohh_year1,na.rm=T),
                          `\\Delta Dem. Exp.`=mean(DemSpExpDiff_nohh,na.rm=T),
                          `\\Delta Rep. Exp.`=mean(RepSpExpDiff_nohh,na.rm=T),
                            `Block Group White`=mean(WhiteBlockGroup_year1,na.rm=T),
                            `Block Group Med. HH Inc.`=round(mean(HHIncomeBlockGroup_year1,na.rm=T)),
                            `Block Group Homeowner`=mean(HomeownerBlockGroup_year1,na.rm=T)
                            
                            
                            
),by='Status']



## p3



d3=p3[!is.na(Status),list(Age=round(mean(Age_year1,na.rm=T)),
                            Female=mean(Female,na.rm=T),

                            White=mean(White,na.rm=T),
                          `2016 Dem. Exp.`=mean(DemSpExp_nohh_year1,na.rm=T),
                          `2016 Rep. Exp.`=mean(RepSpExp_nohh_year1,na.rm=T),
                          `\\Delta Dem. Exp.`=mean(DemSpExpDiff_nohh,na.rm=T),
                          `\\Delta Rep. Exp.`=mean(RepSpExpDiff_nohh,na.rm=T),
                        
                            `Block Group White`=mean(WhiteBlockGroup_year1,na.rm=T),
                            `Block Group Med. HH Inc.`=round(mean(HHIncomeBlockGroup_year1,na.rm=T)),
                            `Block Group Homeowner`=mean(HomeownerBlockGroup_year1,na.rm=T)
                            
                            
                            
),by='Status']



###
t = d1 %>%
  as_tibble %>%
  pivot_longer(Age:`Block Group Homeowner`)%>%
  mutate(Sample = '2008-2012')%>%
  pivot_wider(names_from = 'Status')%>%
  select(Years=Sample, Variable = name,`Stable Democrat`, `Switch Democrat`, `Stable Republican`, `Switch Republican`, `Stable Non-Partisan`, `Switch Non-Partisan`)%>%
  bind_rows(d2 %>%
              as_tibble %>%
              pivot_longer(Age:`Block Group Homeowner`)%>%
              mutate(Sample = '2012-2016')%>%
              pivot_wider(names_from = 'Status')%>%
              select(Years=Sample, Variable = name,`Stable Democrat`, `Switch Democrat`, `Stable Republican`, `Switch Republican`, `Stable Non-Partisan`, `Switch Non-Partisan`))%>%
  bind_rows(d3 %>%
              as_tibble %>%
              pivot_longer(Age:`Block Group Homeowner`)%>%
              mutate(Sample = '2016-2020')%>%
              pivot_wider(names_from = 'Status')%>%
              select(Years=Sample, Variable = name,`Stable Democrat`, `Switch Democrat`, `Stable Republican`, `Switch Republican`, `Stable Non-Partisan`, `Switch Non-Partisan`))
            
            
            
            out = t %>%
              gt()%>%
              fmt_number(columns = 3:8, decimals = 3)%>%
          
              
              
              
              as_latex()%>%
              as.character()%>%
              str_replace_all('longtable','tabular')
            
            write_file(out, 'tables/TabS6.tex')
            
  





